<template>
  <div>
    <my-breadcrumb :items="['商家大全', '闲置设置']"/>
    <search :formItems="searchItems" @search="getList" ref="search"/>
    <div class="add">
      <el-button type="primary" @click="toSetConfig">修改配置</el-button>
    </div>
    <el-table :data="idles">
      <el-table-column
        type="index"
        width="55">
      </el-table-column>
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        label="创建时间"
        prop="createtime">
      </el-table-column>
      <el-table-column
        label="预约日期"
        prop="monid">
      </el-table-column>
      <el-table-column
        label="预约数量"
        prop="count">
      </el-table-column>
      <el-table-column
        label="当前预约量"
        prop="current">
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="toUpdate(scope.row.id, scope.row.count)">修改预约量</el-button>
          <el-button size="mini" type="text" @click="toDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNum"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
    <el-dialog title="配置修改" :visible.sync="showConfig">
      <el-form :model="config" label-width="140px" :rules="rules" ref="addForm">
        <el-form-item label="预约数量" prop="number">
          <el-input-number v-model="config.number" :min="1"/>
        </el-form-item>
        <el-form-item label="展示时间（小时）" prop="time">
          <el-input-number v-model="config.time" :min="0"/>
        </el-form-item>
      </el-form>
      <div class="footer" slot="footer">
        <el-button type="primary" @click="toSubmit">提交</el-button>
        <el-button type="danger" @click="showConfig = false">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {deleteRow} from '../../utils/getList'

export default {
  name: 'idle',
  data () {
    return {
      searchItems: [
        {
          label: '闲置日期',
          type: 'time-one',
          key: 'monid'
        }
      ], // 检索结构
      idles: [],
      page: {
        pageNum: 1,
        pageSize: 20,
        total: 0
      },
      showConfig: false,
      config: {
        id: '',
        number: null,
        time: null
      },
      rules: {
        number: [
          { required: true, message: '请输入数量', trigger: 'blur' }
        ],
        time: [
          { required: true, message: '请输入时间', trigger: 'blur' }
        ]
      },
      other: {}
    }
  },
  methods: {
    getList () {
      let searchData = {}
      if (this.$refs['search']) {
        searchData = this.$refs['search'].search()
      }
      let params = {...searchData, page: this.page.pageNum, size: this.page.pageSize}
      this.axios.get('/Usedlimit/index', {params})
        .then(res => {
          if (res.data.status) {
            this.page.total = Number(res.data.data.count)
            this.idles = res.data.data.list
          }
        })
    },
    handleSizeChange (e) {
      this.page.pageSize = e
      this.getList()
    },
    handleCurrentChange (e) {
      this.page.pageNum = e
      this.getList()
    },

    toDelete (id) {
      deleteRow({url: '/Usedlimit/delete', id}, res => {
        if (res.data.status === '1' || res.data.status === 1) {
          this.getList()
          this.$message.success(res.data.msg)
        }
      })
    },
    toUpdate (id, value) {
      this.$prompt('请输入预约量', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputValue: value,
        inputPattern: /^[1-9]\d*$/
      }).then(({ value }) => {
        this.axios.post('/Usedlimit/edit', this.qs.stringify({id, count: value}))
          .then(res => {
            if (res.data.status === 1) {
              this.getList()
              this.$message.success(res.data.msg)
            }
          })
      })
    },

    toSetConfig () {
      this.showConfig = true
    },
    toSubmit () {
      this.$refs['addForm'].validate((valid) => {
        if (valid) {
          let params = {data: [this.other, this.config]}
          this.axios.post('/Config/editusedconfig', this.qs.stringify(params))
            .then(res => {
              if (res.data.status === 1) {
                this.$message.success(res.data.msg)
                this.showConfig = false
                this.initConfig()
              }
            })
        } else {
          this.$message.error('请检查表单是否填写正确')
          return false
        }
      })
    },
    initConfig () {
      this.axios.get('/Config/getusedconfig')
        .then(res => {
          if (res.data.status === 1) {
            Object.keys(res.data.data[0]).forEach(key => {
              this.config[key] = res.data.data[0][key]
            })
            this.other = res.data.data[1]
          }
        })
    }
  },
  created () {
    this.getList()
    this.initConfig()
  }
}
</script>

<style scoped>

</style>
